package com.iwomy.envpro.platform.utils;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSONObject;
import com.iwomy.envpro.common.core.util.SpringContextHolder;
import com.iwomy.envpro.platform.constant.RedisConstants;
import com.iwomy.envpro.platform.dto.DeviceDataDTO;
import com.iwomy.envpro.platform.entity.TelecomTianyiAiotEntity;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AIOTUtils {
	private static RedisClient redisClient = SpringContextHolder.getBean(RedisClient.class);

	public static String calculateSHA256Signature(String signKey, String masterKey, String timestamp) {
		String input = signKey + masterKey + timestamp;
		Console.log(input);
		return SecureUtil.sha256(input);
	}


	public static String getAccessToken(TelecomTianyiAiotEntity telecomTianyiAiot) {
		String redisKey = RedisConstants.TIANYI_AIOT_ACCESS_TOKEN + telecomTianyiAiot.getImei();
		String accessToken = redisClient.get(redisKey);
		if (StrUtil.isNotEmpty(accessToken)) return accessToken;
		String signKey = telecomTianyiAiot.getSignKey();
		String timestamp = System.currentTimeMillis() + "";
		String signature = calculateSHA256Signature(signKey,
				telecomTianyiAiot.getMasterKey(), timestamp);
		HttpRequest request = HttpUtil.createRequest(Method.POST, "http://" + telecomTianyiAiot.getTianyiTenantId() + ".non-nb.ctwing.cn:8991/auth");

		request.header("timestamp", timestamp);
		request.header("signKey", signKey);
//		request.header("Authorization","token");
		request.header("signature", signature);
		request.header("Content-Type", "application/json");

		JSONObject body = new JSONObject();
		body.put("productId", telecomTianyiAiot.getProductId());
		body.put("deviceId", telecomTianyiAiot.getDeviceId());
		body.put("password", telecomTianyiAiot.getPassword());
		log.info("getAccessToken body:{}", body.toJSONString());
		HttpResponse execute = request.body(body.toString()).execute();
		String result = execute.body();
		Console.log(result);
		accessToken = JSONObject.parseObject(result).getString("accessToken");
		redisClient.set(redisKey, accessToken, 5 * 60 * 1000);
		return accessToken;
	}

	public static void report(TelecomTianyiAiotEntity telecomTianyiAiot, DeviceDataDTO deviceDataDTO) {

		String accessToken = getAccessToken(telecomTianyiAiot);
		if (StrUtil.isEmpty(accessToken)) return;

		String timestamp = System.currentTimeMillis() + "";
		String signKey = telecomTianyiAiot.getSignKey();
		String signature = calculateSHA256Signature(signKey,
				telecomTianyiAiot.getMasterKey(), timestamp);
		HttpRequest request = HttpUtil.createRequest(Method.POST, "http://" + telecomTianyiAiot.getTianyiTenantId() + ".non-nb.ctwing.cn:8991/topic");
		request.header("timestamp", timestamp);
		request.header("signKey", signKey);
		request.header("signature", signature);
		request.header("Content-Type", "application/json");
//		JSONObject payload = new JSONObject();
//		payload.put("smoke_state", "0");
//		payload.put("smoke_value", "60");
//		payload.put("battery_value", "100");
//		payload.put("snr", "15");
		JSONObject body = new JSONObject();
		body.put("productId", telecomTianyiAiot.getProductId());
		body.put("deviceId", telecomTianyiAiot.getDeviceId());
		body.put("topic", "data_report");
		body.put("accessToken", accessToken);


		body.put("payload", deviceDataDTO);
		log.info("report body:{}", body.toJSONString());
		HttpResponse execute = request.body(body.toString()).execute();
		String result = execute.body();
		log.info(result);


	}


	public static void main(String[] args) {
		String signKey = "123456";
		String masterKey = "8574380304de4efc87e702ebe353ce00";
		String timestamp = System.currentTimeMillis() + "";

		String signature = calculateSHA256Signature(signKey, masterKey, timestamp);
		Console.log("Signature: " + signature);
		HttpRequest request = HttpUtil.createRequest(Method.POST, "http://2000436958.non-nb.ctwing.cn:8991/auth");

		request.header("timestamp", timestamp);
		request.header("signKey", signKey);
//		request.header("Authorization","token");
		request.header("signature", signature);
		request.header("Content-Type", "application/json");

		JSONObject body = new JSONObject();
		body.put("productId", 17020539);
		body.put("deviceId", "1702053920230613000002");
		body.put("password", "R0_lXi4W6ypIpbozUzgUO4MmIvltstQAzMhoJT49rJk");


		HttpResponse execute = request.body(body.toString()).execute();
		String result = execute.body();
		Console.log(result);
		String accessToken = JSONObject.parseObject(result).getString("accessToken");

		request = HttpUtil.createRequest(Method.POST, "http://2000436958.non-nb.ctwing.cn:8991/topic");
		request.header("timestamp", timestamp);
		request.header("signKey", signKey);
//		request.header("Authorization","token");
		request.header("signature", signature);
		request.header("Content-Type", "application/json");

		JSONObject payload = new JSONObject();
		payload.put("smoke_state", "0");
		payload.put("smoke_value", "60");
		payload.put("battery_value", "100");
		payload.put("snr", "15");

		body = new JSONObject();
		body.put("productId", 17020539);
		body.put("deviceId", "1702053920230613000002");
		body.put("topic", "data_report");
		body.put("accessToken", accessToken);
		body.put("payload", payload);
		execute = request.body(body.toString()).execute();
		result = execute.body();
		Console.log(result);


	}
}
